package com.genia.toolbox.projects.tests.testSuppressReadOnlyTransactions.manager;

import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.manager.PersistenceManager;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicString;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicText;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nString;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nText;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nDynamicStringImpl;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nDynamicTextImpl;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nStringImpl;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nTextImpl;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.LanguageImpl;

/**
 * utility class for i18n fields for tests.
 */
public class I18nTestUtil
    extends AbstractDAO
{

  /**
   * the list of {@link Locale} used during the test.
   */
  private static final Locale[] LOCALES_TEST = new Locale[] { Locale.ENGLISH, Locale.FRENCH };



  @Override
  @Resource
  public void setPersistenceManager(PersistenceManager persistenceManager)
  {
    super.setPersistenceManager(persistenceManager);
  }



  /**
   * init and save languages for the test.
   * 
   * @throws BundledException
   *                 if an error occur
   */  
  public void initLanguages()
      throws BundledException
  {
    for (int i = 0; i < LOCALES_TEST.length; i++) {
      Locale locale = LOCALES_TEST[i];
      if (getLanguage(locale) == null) {
        LanguageImpl language = new LanguageImpl();
        language.setLocale(locale);
        language.setOrder(i);
        save(language);
      }
    }

  }



  /**
   * get the  {@link com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language} that corresponds to the given {@link Locale}.
   * 
   * @param locale
   *                the locale to consider
   * @return the corresponding  {@link com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language}
   * @throws BundledException
   *                 if an error occur
   */
  public Language getLanguage(Locale locale)
      throws BundledException
  {
    List<Language> languages = getAll(Language.class);
    Language result = null;
    for (Language language : languages) {
      if (language.getLocale().equals(locale)) {
        result = language;
        break;
      }
    }
    return result;
  }



  /**
   * create an {@link I18nDynamicText} object.
   * 
   * @param locale
   *                the related locale
   * @param value
   *                the string value
   * @return an {@link I18nDynamicText} instance
   * @throws BundledException
   *                 if an error occur
   */
  public I18nDynamicText initDynamicText(Locale locale, String value)
      throws BundledException
  {

    I18nDynamicTextImpl dynamicText = new I18nDynamicTextImpl();
    I18nText i18nText = new I18nTextImpl();
    i18nText.setLanguage(getLanguage(locale));
    i18nText.setValue(value);
    dynamicText.addI18nValue(i18nText);
    return dynamicText;

  }



  /**
   * create an {@link I18nDynamicText} object.
   * 
   * @param locales
   *                an array of {@link Locale}s
   * @param values
   *                an array of {@link String} values
   * @return an {@link I18nDynamicText} instance
   * @throws BundledException
   *                 if an error occur
   */
  public I18nDynamicText initDynamicText(Locale[] locales, String[] values)
      throws BundledException
  {
    I18nDynamicTextImpl dynamicText = new I18nDynamicTextImpl();
    for (int i = 0; i < locales.length; i++) {
      I18nText i18nText = new I18nTextImpl();
      i18nText.setLanguage(getLanguage(locales[i]));
      i18nText.setValue(values[i]);
      dynamicText.addI18nValue(i18nText);
    }
    return dynamicText;

  }



  /**
   * create an {@link I18nDynamicString} object.
   * 
   * @param locale
   *                the related locale
   * @param value
   *                the string value
   * @return an {@link I18nDynamicString} instance
   * @throws BundledException
   *                 if an error occur
   */
  public I18nDynamicString initDynamicString(Locale locale, String value)
      throws BundledException
  {
    I18nDynamicStringImpl dynamicString = new I18nDynamicStringImpl();
    I18nString i18nString = new I18nStringImpl();
    i18nString.setLanguage(getLanguage(locale));
    i18nString.setValue(value);
    dynamicString.addI18nValue(i18nString);
    return dynamicString;
  }



  /**
   * create an {@link I18nDynamicString} object.
   * 
   * @param locales
   *                an array of {@link Locale}s
   * @param values
   *                an array of {@link String} values
   * @return an {@link I18nDynamicString} instance
   * @throws BundledException
   *                 if an error occur
   */
  public I18nDynamicString initDynamicString(Locale[] locales, String[] values)
      throws BundledException
  {
    I18nDynamicStringImpl dynamicString = new I18nDynamicStringImpl();
    for (int i = 0; i < locales.length; i++) {
      I18nString i18nString = new I18nStringImpl();
      i18nString.setLanguage(getLanguage(locales[i]));
      i18nString.setValue(values[i]);
      dynamicString.addI18nValue(i18nString);
    }
    return dynamicString;
  }

}
